<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Validate;
use app\facade\Redis;
use app\models\{User as Users, HallTrade, Machine, MachineLog, HallApple, Config, ContractList, ContractOrder, ContractReser, ContractSell, ContractApply};

class Hall extends Base
{

    public function minging()
    {
        $model = new Machine();
        $data = $this->request->get();
        $list = $model->order('id desc');
        $list = $list->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }


    public function minging_edit()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new Machine();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0'
            ], [
                'id.require' => 'ID不能为空',
                'id.gt' => 'ID必须大于0'
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            //判断手机号码是否已存在
            $user = $model->where(['id' => $data['id']])->find();
            if (empty($user)) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息不存在']);
            }
            Db::startTrans();
            try {
                $model->allowfield(true)->save($data, ['id' => $data['id']]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '编辑失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '编辑成功']);
        } else {
            $id = $this->request->get('id/d', 0);
            $list = (new Machine())->where('id', $id)->find();
            return view('', ['row' => $list]);
        }
    }

    public function userminging()
    {
        $model = new MachineLog();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (isset($data['status']) && $data['status']) {
            $list->where('status', $data['status']);
        }
        if (isset($data['type']) && $data['type']) {
            $list->where('type', $data['type']);
        }
        if (isset($data['mining_id']) && $data['mining_id']) {
            $list->where('mining_id', $data['mining_id']);
        }
        if (!empty($data['mobile'])) {
            $user_id = (new Users())->where('id|mobile', $data['mobile'])->value('id');
            $list = $list->where('buyer_id', $user_id);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($item) {
            $item['mobile'] = (new Users())->where('id', $item['user_id'])->value('mobile');
            $item['machine'] = (new Machine())->where('id', $item['mining_id'])->value('name');
            return $item;
        });
        $machine = (new Machine())->select();
        return view('', ['list' => $list, 'machine' => $machine]);
    }

    public function usermingingstr()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new MachineLog();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0'
            ], [
                'id.require' => 'ID不能为空',
                'id.gt' => 'ID必须大于0'
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            //判断手机号码是否已存在
            $user = $model->where(['id' => $data['id']])->find();
            if (empty($user)) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息不存在']);
            }
            if ($user['status'] == 2) {
                return json(['status' => 1, 'data' => [], 'msg' => '已结束']);
            }
            Db::startTrans();
            try {
                $model->where(['id' => $data['id']])->update(['status' => 2, 'text' => '后台操作结束']);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '编辑失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '编辑成功']);
        }
    }

    public function halls()
    {
        $model = new HallTrade();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (isset($data['start']) && $data['start']) {
            $list->where('created', '>=', $data['start']);
        }
        if (isset($data['endtime']) && $data['endtime']) {
            $list->where('created', '<=', $data['endtime']);
        }
        if (isset($data['status']) && $data['status']) {
            $list->where('status', $data['status']);
        }
        if (isset($data['type']) && $data['type']) {
            $list->where('type', $data['type']);
        }
        if (!empty($data['mobile'])) {
            $user_id = (new Users())->where('id|mobile', $data['mobile'])->value('id');
            $list = $list->where('buyer_id', $user_id);
        }
        if (!empty($data['pmobile'])) {
            $puser_id = (new Users())->where('id|mobile', $data['pmobile'])->value('id');
            $list = $list->where('seller_id', $puser_id);
        }
        if (!empty($data['ordersn'])) {
            $ordersn =  $data['ordersn'] - pow(12, 10);
            $list = $list->whereor('id', $data['ordersn'])->whereor('id', $ordersn);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($item) {
            if ($item['buyer_id'] > 0) {
                $item['mobile'] = (new Users())->where('id', $item['buyer_id'])->value('mobile');
            }
            if ($item['seller_id'] > 0) {
                $item['pmobile'] = (new Users())->where('id', $item['seller_id'])->value('mobile');
            }
            $item['ordersn'] =  pow(12, 10) + $item['id'];
            return $item;
        });
        return view('', ['list' => $list]);
    }

    public function hallsstr()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new HallTrade();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0',
                'status' => 'require'
            ], [
                'id.require' => 'ID不能为空',
                'id.gt' => 'ID必须大于0',
                'status.require' => '订单状态必须选择',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            //判断手机号码是否已存在
            $order = $model->where(['id' => $data['id']])->find();
            if (empty($order)) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息不存在']);
            }
            if ($order['status'] != 1 && $order['status'] != 2 && $order['status'] != 3) {
                return json(['status' => 1, 'data' => [], 'msg' => '订单状态无法修改']);
            }
            if ($data['status'] == 4 && $order['status'] == 1) {
                return json(['status' => 1, 'data' => [], 'msg' => '未匹配订单无法确认']);
            }
            if ($data['status'] == 4) {
                $buyuser = (new Users())->where('id', $order['buyer_id'])->find();
                $quota = 0;
                $quotaratio = (new Config())->where('key', 'hallquote')->value('value');
                $quotaratio =  $quotaratio ?? 1.5;
                $quota = round($order['num'] * $quotaratio, 2);
            }
            $suser = (new Users())->where('id', $order['seller_id'])->find();
            Db::startTrans();
            try {
                $model->where(['id' => $data['id']])->update(['status' => $data['status']]);
                if ($order['status'] == 2 || $order['status'] == 3) {
                    //通过
                    if ($data['status'] == 4 && !empty($buyuser) && $order['num'] > 0) {
                        (new Users())->MoneyEdit($buyuser['id'], $buyuser['money'], $order['num'], 1, 2, 'OTC购买[系统]', $order['id']);
                        //额度操作
                        (new Users())->MoneyEdit($buyuser['id'], $buyuser['quota'], $quota, 3, 1, 'OTC购买[系统]', $order['id']);
                    }
                    //驳回
                    if ($data['status'] == 5) {
                        (new Users())->MoneyEdit($order['seller_id'], $suser['amount'], $order['num'], 2, 3, '取消订单[系统]', $order['id'], 0, 0, 0);
                        //手续费操作
                        if ($order['handfee'] > 0) {
                            (new Users())->MoneyEdit($order['seller_id'], ($suser['amount'] + $order['num']), $order['handfee'], 2, 4, '取消挂单手续费[系统]', $order['id'], 0, 0, 0);
                        }
                        //额度操作
                        (new Users())->MoneyEdit($order['seller_id'], $suser['quota'], $order['num'], 3, 2, '售卖挂单取消[系统]', $order['id'], 0, 0, 0);
                    }
                }
                Db::commit();
            } catch (\Exception $e) {
                dump($e->getMessage());
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
    }


    public function hallapply()
    {
        $model = new HallApple();
        $data = $this->request->get();
        $list = $model->where('type', 1)->order('id desc');
        if (isset($data['status']) && $data['status']) {
            $list->where('status', $data['status']);
        }
        if (!empty($data['ordersn'])) {
            $ordersn =  $data['ordersn'] - pow(12, 10);
            $list = $list->whereor('hallid', $data['ordersn'])->whereor('id', $ordersn);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($item) {
            $item['images'] = json_decode($item['images'], true);
            $item['ordersn'] =  pow(12, 10) + $item['id'];
            return $item;
        });
        return view('', ['list' => $list]);
    }

    public function hallapplystr()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new HallTrade();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0',
                'status' => 'require'
            ], [
                'id.require' => 'ID不能为空',
                'id.gt' => 'ID必须大于0',
                'status.require' => '订单状态必须选择',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            //判断手机号码是否已存在
            $apple = (new HallApple())->where(['id' => $data['id'], 'type' => 1])->find();
            if (empty($apple)) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息不存在']);
            }
            if ($apple['status'] == 2) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息已审核']);
            }
            $order = $model->where(['id' => $apple['hallid']])->find();
            if ($order['status'] != 6 && $order['status'] != 7) {
                return json(['status' => 1, 'data' => [], 'msg' => '订单状态无法审核']);
            }
            $buyuser = (new Users())->where('id', $order['buyer_id'])->find();
            $quota = 0;
            $quotaratio = (new Config())->where('key', 'hallquote')->value('value');
            $quotaratio =  $quotaratio ?? 1.5;
            $quota = round($order['num'] * $quotaratio, 2);
            $suser = (new Users())->where('id', $order['seller_id'])->find();
            Db::startTrans();
            try {
                (new HallApple())->where(['id' => $data['id'], 'type' => 1])->update(['status' => 2]);
                $model->where(['id' => $order['id']])->update(['status' => $data['status']]);
                //通过
                if ($data['status'] == 4 && !empty($buyuser) && $order['num'] > 0) {
                    (new Users())->MoneyEdit($buyuser['id'], $buyuser['money'], $order['num'], 1, 2, 'OTC购买[系统]', $order['id']);
                    //额度操作
                    (new Users())->MoneyEdit($buyuser['id'], $buyuser['quota'], $quota, 3, 1, 'OTC购买[系统]', $order['id']);
                }
                //驳回
                if ($data['status'] == 5) {
                    (new Users())->MoneyEdit($order['seller_id'], $suser['amount'], $order['num'], 2, 3, '取消订单[系统]', $order['id'], 0, 0, 0);
                    //手续费操作
                    if ($order['handfee'] > 0) {
                        (new Users())->MoneyEdit($order['seller_id'], ($suser['amount'] + $order['num']), $order['handfee'], 2, 4, '取消挂单手续费[系统]', $order['id'], 0, 0, 0);
                    }
                    //额度操作
                    (new Users())->MoneyEdit($order['seller_id'], $suser['quota'], $order['num'], 3, 2, '售卖挂单取消[系统]', $order['id'], 0, 0, 0);
                }
                Db::commit();
            } catch (\Exception $e) {
                dump($e->getMessage());
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
    }

    public function contact()
    {
        $model = new ContractList();
        $data = $this->request->get();
        $list = $model->order('id desc');
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($item) {
            $item['count'] = (new ContractSell())->where(['product_id' => $item['id']])->count(); //市场发行总量
            $item['count1'] = (new ContractSell())->where(['product_id' => $item['id'], 'status' => 4])->count(); //已报销
            $item['count2'] = (new ContractSell())->where(['product_id' => $item['id'], 'status' => 5])->count(); //用户兑换
            $item['count3'] = (new ContractOrder())->where(['product_id' => $item['id'], 'status' => 3, 'income_status' => 2])->count(); //收益中
            $item['count4'] = (new ContractSell())->where(['product_id' => $item['id'], 'status' => 1])->count(); //待匹配数量
            $item['count5'] = (new ContractSell())->where(['product_id' => $item['id'], 'status' => 2])->count(); //匹配中
            $item['count6'] = (new ContractReser())->where(['cate_id' => $item['id'], 'status' => 1])->count(); //预约数量
            $item['count7'] =  $item['count3'] + $item['count4'] + $item['count5']; //平台实际真实数量
            $item['count8'] = (new ContractSell())->where(['product_id' => $item['id'], 'status' => [1, 2, 3]])->where('created', '>=', date('Y-m-d'))->count();
            return $item;
        });
        return view('', ['list' => $list]);
    }



    public function contactliststr()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new ContractList();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0'
            ], [
                'id.require' => 'ID不能为空',
                'id.gt' => 'ID必须大于0'
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            //判断手机号码是否已存在
            $user = $model->where(['id' => $data['id']])->find();
            if (empty($user)) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息不存在']);
            }
            Db::startTrans();
            try {
                $model->allowfield(true)->save($data, ['id' => $data['id']]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '编辑失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '编辑成功']);
        } else {
            $id = $this->request->get('id/d', 0);
            $list = (new ContractList())->where('id', $id)->find();
            return view('', ['row' => $list]);
        }
    }

    //待匹配合约
    public function contacting()
    {
        $model = new ContractSell();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (isset($data['status']) && $data['status']) {
            $list->where('status', $data['status']);
        }
        if (!empty($data['mobile'])) {
            $user_id = (new Users())->where('id|mobile', $data['mobile'])->value('id');
            $list = $list->where('buyer_id', $user_id);
        }
        if (!empty($data['product_id'])) {
            $list = $list->where('product_id', $data['product_id']);
        }
        if (!empty($data['ordersn'])) {
            $ordersn =  $data['orderid'] - pow(12, 10);
            $list = $list->whereor('orderid', $data['ordersn'])->whereor('orderid', $ordersn);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($item) {
            $item['mobile'] = (new Users())->where('id', $item['user_id'])->value('mobile');
            $item['ordersn'] =  pow(12, 10) + $item['orderid'];
            $item['toordersn'] =  pow(12, 10) + $item['toorderid'];
            return $item;
        });
        $contrt = (new ContractList())->select();
        return view('', ['list' => $list, 'contrt' => $contrt]);
    }

    //预约列表
    public function yuyue()
    {
        $model = new ContractReser();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (isset($data['status']) && $data['status']) {
            $list->where('status', $data['status']);
        }
        if (!empty($data['mobile'])) {
            $user_id = (new Users())->where('id|mobile', $data['mobile'])->value('id');
            $list = $list->where('buyer_id', $user_id);
        }
        if (!empty($data['product_id'])) {
            $list = $list->where('cate_id', $data['product_id']);
        }
        if (!empty($data['sellid'])) {
            $list = $list->where('sellid', $data['sellid']);
        }
        if (!empty($data['type'])) {
            $list = $list->where('type', $data['type']);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($item) {
            $item['mobile'] = (new Users())->where('id', $item['user_id'])->value('mobile');
            return $item;
        });
        $contrt = (new ContractList())->select();
        return view('', ['list' => $list, 'contrt' => $contrt]);
    }

    public function yuyuestr()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new ContractReser();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0',
                'status' => 'require'
            ], [
                'id.require' => 'ID不能为空',
                'id.gt' => 'ID必须大于0',
                'status.require' => '订单状态必须选择',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            $order = $model->where(['id' => $data['id']])->find();
            if (empty($order)) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息不存在']);
            }
            if ($order['status'] != 1) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息已匹配']);
            }
            Db::startTrans();
            try {
                $model->where(['id' => $data['id']])->update(['bixu' => $data['status']]);
                Db::commit();
            } catch (\Exception $e) {
                dump($e->getMessage());
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
    }

    public function contactgiveing()
    {
        $model = new ContractOrder();
        $data = $this->request->get();
        $list = $model->order('id desc')->where(['status' => 3, 'income_status' => 2]);
        if (!empty($data['mobile'])) {
            $user_id = (new Users())->where('id|mobile', $data['mobile'])->value('id');
            $list = $list->where('user_id', $user_id);
        }
        if (!empty($data['pmobile'])) {
            $suser_id = (new Users())->where('id|mobile', $data['mobile'])->value('id');
            $list = $list->where('seller_id', $suser_id);
        }
        if (!empty($data['product_id'])) {
            $list = $list->where('product_id', $data['product_id']);
        }
        if (!empty($data['ordersn'])) {
            $ordersn =  $data['orderid'] - pow(12, 10);
            $list = $list->whereor('id', $data['ordersn'])->whereor('id', $ordersn);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($item) {
            $item['mobile'] = (new Users())->where('id', $item['user_id'])->value('mobile');
            if ($item['seller_id']) {
                $item['pmobile'] = (new Users())->where('id', $item['seller_id'])->value('mobile');
            }
            $item['ordersn'] = pow(12, 10) + $item['id'];
            return $item;
        });
        $contrt = (new ContractList())->select();
        return view('', ['list' => $list, 'contrt' => $contrt]);
    }


    public function contactgiveingstr()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new ContractOrder();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0',
                'type' => 'require|in:1,2',
            ], [
                'id.require' => 'ID不能为空',
                'id.gt' => 'ID必须大于0',
                'type.require' => '类型不能为空',
                'type.in' => '类型不正确',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            //判断手机号码是否已存在
            $lists = $model->where(['id' => $data['id'], 'status' => 3, 'income_status' => 2])->find();
            if (empty($lists)) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息不存在']);
            }
            Db::startTrans();
            try {
                $model->where(['id' => $data['id'], 'status' => 3, 'income_status' => 2])->update(['income_status' => 3]);
                if ($data['type'] == 1) {
                    (new ContractSell())->insert([
                        'user_id' => $lists['user_id'],
                        'product_id' => $lists['product_id'],
                        'product_name' => $lists['product_name'],
                        'orderid' => $lists['id'],
                        'buyprice' => $lists['product_price'],
                        'price' => $lists['price'],
                        'status' => 4
                    ]);
                }
                if ($data['type'] == 2) {
                    (new ContractSell())->insert([
                        'user_id' => $lists['user_id'],
                        'product_id' => $lists['product_id'],
                        'product_name' => $lists['product_name'],
                        'orderid' => $lists['id'],
                        'buyprice' => $lists['product_price'],
                        'price' => $lists['price'],
                        'status' => 1
                    ]);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '编辑失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '编辑成功']);
        } else {
            $id = $this->request->get('id/d', 0);
            $list = (new ContractList())->where('id', $id)->find();
            return view('', ['row' => $list]);
        }
    }


    public function contactdel()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new ContractSell();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0',
                'status' => 'require|in:2,4',
            ], [
                'id.require' => 'ID不能为空',
                'id.gt' => 'ID必须大于0',
                'status.require' => '类型不能为空',
                'status.in' => '类型不正确',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            if ($data['status'] == 2 && empty($data['mobile'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '请输入用户账号']);
            }

            //判断手机号码是否已存在
            $lists = $model->where(['id' => $data['id'], 'status' => 1])->find();
            if (empty($lists)) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息不存在或状态不可更改']);
            }
            if ($data['status'] == 2 && !empty($data['mobile'])) {
                $mobile = (new Users())->where('mobile', $data['mobile'])->find();
                if (empty($mobile)) {
                    return json(['status' => 1, 'data' => [], 'msg' => '用户账号不存在']);
                }
                if ($mobile['id'] == $lists['user_id']) {
                    return json(['status' => 1, 'data' => [], 'msg' => '不能匹配给同一用户']);
                }
                $contract =  (new ContractList())->where(['id' => $lists['product_id']])->find();
            }
            Db::startTrans();
            try {
                if ($data['status'] == 2) {
                    $id = (new ContractOrder())->insertGetId([
                        'user_id' => $mobile['id'],
                        'seller_id' => $lists['user_id'],
                        'product_id' => $lists['product_id'],
                        'product_name' => $lists['product_name'],
                        'matchid' => $lists['id'],
                        'product_price' => $lists['price'],
                        'integral' => 0,
                        'price' => $lists['price'],
                        'maxprice' => $contract['max_price'],
                        'days' => $contract['cycle'],
                        'figure' => $contract['income'],
                        'idays' => $contract['cycle'],
                        'created' => date('Y-m-d H:i:s'),
                        'incometime' =>  date('Y-m-d H:i:s'),
                        'income_status' => 1,
                        'status' => 1
                    ]);
                    $model->where(['id' => $data['id'], 'status' => 1])->update(['status' => $data['status'], 'toorderid' => $id]);
                } else {
                    $model->where(['id' => $data['id'], 'status' => 1])->update(['status' => $data['status']]);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '编辑失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '编辑成功']);
        } else {
            $id = $this->request->get('id/d', 0);
            $list = (new ContractList())->where('id', $id)->find();
            return view('', ['row' => $list]);
        }
    }

    public function contactings()
    {
        $model = new ContractOrder();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (!empty($data['mobile'])) {
            $user_id = (new Users())->where('id|mobile', $data['mobile'])->value('id');
            $list = $list->where('user_id', $user_id);
        }
        if (!empty($data['pmobile'])) {
            $suser_id = (new Users())->where('id|mobile', $data['mobile'])->value('id');
            $list = $list->where('seller_id', $suser_id);
        }
        if (!empty($data['product_id'])) {
            $list = $list->where('product_id', $data['product_id']);
        }
        if (!empty($data['status'])) {
            $list = $list->where('status', $data['status']);
        }
        if (!empty($data['matchid'])) {
            $list = $list->where('matchid', $data['matchid']);
        }
        if (!empty($data['ordersn'])) {
            $ordersn =  $data['ordersn'] - pow(12, 10);
            $list = $list->whereor('id', $data['ordersn'])->whereor('id', $ordersn);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($item) {
            $item['mobile'] = (new Users())->where('id', $item['user_id'])->value('mobile');
            if ($item['seller_id']) {
                $item['pmobile'] = (new Users())->where('id', $item['seller_id'])->value('mobile');
            }
            $item['ordersn'] = pow(12, 10) + $item['id'];
            return $item;
        });
        $contrt = (new ContractList())->select();
        return view('', ['list' => $list, 'contrt' => $contrt]);
    }


    public function contactadd()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new ContractList();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0',
                'mobile' => 'require',
                'money' => 'require|gt:0',
                'number' => 'require|number|gt:0'
            ], [
                'id.require' => '用户ID不能为空',
                'id.gt' => '用户必须大于0',
                'mobile.require' => '手机账号不能为空',
                'money.require' => '合约面值不能为空',
                'money.gt' => '合约面值必须大于0',
                'number.require' => '合约数量不能为空',
                'number.number' => '合约数量必须是整数',
                'number.gt' => '合约数量必须大于0',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            $contract = $model->where(['id' => $data['id']])->find();
            if (empty($contract)) {
                return json(['status' => 1, 'data' => [], 'msg' => '合约信息不存在']);
            }
            $haveuser = (new Users())->where(['mobile' => $data['mobile']])->find();
            if (empty($haveuser)) {
                return json(['status' => 1, 'data' => [], 'msg' => '手机账号不存在']);
            }
            $number = (int)$data['number'];
            $arr = [];
            for ($i = 0; $i < $number; $i++) {
                $arr[] =  [
                    'user_id' => $haveuser['id'],
                    'product_id' => $contract['id'],
                    'product_name' => $contract['name'],
                    'buyprice' => $data['money'],
                    'price' => $data['money'],
                    'status' => 1
                ];
            }
            Db::startTrans();
            try {
                (new ContractSell())->insertAll($arr);
                Db::commit();
            } catch (\Exception $e) {
                dump($e->getMessage());
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '修改失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '修改成功']);
        } else {
            $contract = (new ContractList())->select();
            return view('', ['contract' => $contract]);
        }
    }

    public function contactstr()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new ContractOrder();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0',
                'status' => 'require'
            ], [
                'id.require' => 'ID不能为空',
                'id.gt' => 'ID必须大于0',
                'status.require' => '订单状态必须选择',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            $order = $model->where(['id' => $data['id']])->find();
            if (empty($order)) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息不存在']);
            }
            if ($order['status'] != 1 && $order['status'] != 2) {
                return json(['status' => 1, 'data' => [], 'msg' => '订单状态无法修改']);
            }
            $sell = (new ContractSell())->where('id', $order['matchid'])->find();
            //卖家收益计算
            $money = $sell['price'] - $sell['buyprice'];
            Db::startTrans();
            try {
                if ($data['status'] == 3) {
                    $model->where(['id' => $data['id']])->update(['status' => 3, 'income_status' => 2]);
                    (new ContractSell())->where(['id' => $order['matchid'], 'status' => 2])->update(['status' => 3]);
                    if ($money > 0) {
                        (new Users())->where('id', $sell['user_id'])->setInc('cincome', $money);
                    }
                }
                if ($data['status'] == 4) {
                    $model->where(['id' => $data['id']])->update(['status' => 4]);
                    (new ContractSell())->where(['id' => $order['matchid'], 'status' => 2])->update(['status' => 1]);
                }
                if ($data['status'] == 5) {
                    $model->where(['id' => $data['id']])->update(['status' => 4]);
                    (new ContractSell())->where(['id' => $order['matchid'], 'status' => 2])->update(['status' => 4]);
                }
                Db::commit();
            } catch (\Exception $e) {
                dump($e->getMessage());
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
    }



    public function conapple()
    {
        $model = new HallApple();
        $data = $this->request->get();
        $list = $model->where('type', 2)->order('id desc');
        if (isset($data['status']) && $data['status']) {
            $list->where('status', $data['status']);
        }
        if (!empty($data['ordersn'])) {
            $ordersn =  $data['ordersn'] - pow(12, 10);
            $list = $list->whereor('hallid', $data['ordersn'])->whereor('id', $ordersn);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($item) {
            $item['images'] = json_decode($item['images'], true);
            $item['ordersn'] =  pow(12, 10) + $item['id'];
            return $item;
        });
        return view('', ['list' => $list]);
    }

    public function conapplestr()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new ContractOrder();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0',
                'status' => 'require'
            ], [
                'id.require' => 'ID不能为空',
                'id.gt' => 'ID必须大于0',
                'status.require' => '订单状态必须选择',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            //判断手机号码是否已存在
            $apple = (new HallApple())->where(['id' => $data['id'], 'type' => 2])->find();
            if (empty($apple)) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息不存在']);
            }
            if ($apple['status'] == 2) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息已审核']);
            }
            $order = $model->where(['id' => $apple['hallid']])->find();
            if (empty($order)) {
                return json(['status' => 1, 'data' => [], 'msg' => '订单不存在']);
            }
            if ($order['status'] != 5 && $order['status'] != 6) {
                return json(['status' => 1, 'data' => [], 'msg' => '订单状态非申诉无法审核']);
            }
            $sell = (new ContractSell())->where('id', $order['matchid'])->find();
            //卖家收益计算
            $money = $sell['price'] - $sell['buyprice'];
            Db::startTrans();
            try {
                (new HallApple())->where(['id' => $data['id']])->update(['status' => 2]);
                if ($data['status'] == 3) {
                    $model->where(['id' => $order['id']])->update(['status' => 3, 'income_status' => 2]);
                    (new ContractSell())->where(['id' => $order['matchid'], 'status' => 2])->update(['status' => 3]);
                    if ($money > 0) {
                        (new Users())->where('id', $sell['user_id'])->setInc('cincome', $money);
                    }
                }
                if ($data['status'] == 4) {
                    $model->where(['id' => $order['id']])->update(['status' => 4]);
                    (new ContractSell())->where(['id' => $order['matchid'], 'status' => 2])->update(['status' => 1]);
                }
                Db::commit();
            } catch (\Exception $e) {
                dump($e->getMessage());
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
    }


    public function conapply()
    {
        $model = new ContractApply();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (!empty($data['mobile'])) {
            $user_id = (new Users())->where('id|mobile', $data['mobile'])->value('id');
            $list = $list->where('user_id', $user_id);
        }
        if (!empty($data['contractid'])) {
            $list = $list->where('contractid', $data['contractid']);
        }
        if (!empty($data['status'])) {
            $list = $list->where('status', $data['status']);
        }
        if (!empty($data['type'])) {
            $list = $list->where('type', $data['type']);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($item) {
            $item['mobile'] = (new Users())->where('id', $item['user_id'])->value('mobile');
            if ($item['contractid'] > 0) {
                $item['name'] = (new ContractList())->where('id', $item['contractid'])->value('name');
            }
            return $item;
        });
        $contrt = (new ContractList())->select();
        return view('', ['list' => $list, 'contrt' => $contrt]);
    }


    public function conapplystr()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $model = new ContractApply();
            $data = $this->request->post();
            $validate = new Validate([
                'id' => 'require|gt:0',
                'status' => 'require'
            ], [
                'id.require' => 'ID不能为空',
                'id.gt' => 'ID必须大于0',
                'status.require' => '订单状态必须选择',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            //判断手机号码是否已存在
            $apple = $model->where(['id' => $data['id']])->find();
            if (empty($apple)) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息不存在']);
            }
            if ($apple['status'] != 1) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息已审核']);
            }

            Db::startTrans();
            try {
                $model->where(['id' => $data['id']])->update(['status' => $data['status'], 'givetime' => date('Y-m-d', time() + 86400)]);
                Db::commit();
            } catch (\Exception $e) {
                dump($e->getMessage());
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
    }
}
